<template>
  <div class="order">
    <div class="header">
      <div class="left">客商列表</div>
      <img src="@/assets/img/fork.svg" class="right" @click="cusCancel" />
    </div>
    <div class="cusBoxHeader">
      <div class="cusSwitchBox">
        <div
          class="labelName"
          :style="{
            background: selected == '2' ? '#169bd5' : 'white',
            color: selected == '2' ? 'white' : 'black',
            border: selected == '2' ? '1px solid #169bd5' : '1px solid #000000'
          }"
          @click="switchCus('2')"
        >
          供应商
        </div>
        <div
          class="labelName"
          :style="{
            background: selected == '1' ? '#169bd5' : 'white',
            color: selected == '1' ? 'white' : 'black',
            border: selected == '1' ? '1px solid #169bd5' : '1px solid #000000'
          }"
          @click="switchCus('1')"
        >
          客户
        </div>
      </div>
      <div class="btn" @click="toAddCus">{{ selected == '2' ? '新增供应商' : '新增客户' }}</div>
    </div>
    <van-pull-refresh v-model="loading" @refresh="getList" class="list">
      <div class="rows" v-for="(rows, index) in cusResList" :key="index">
        <div class="row" v-for="item in rows" :key="item.cusNo">
          <div class="msg">
            <div class="title">
              <div class="no blue" @click.stop="toDetail(item)">
                {{ item.cusNo || '' }}
              </div>
              <div class="no maxWidth">名称： {{ item.cusName || '' }}</div>
              <div class="no">性质： {{ item.cusClassifyName || '' }}</div>
            </div>
            <div class="middle">
              {{ item.cusClassify == '2' ? '身份证号：' : '统一社会信用代码：'
              }}{{ item.cusClassify == '2' ? item.cusIdCard : item.cusTaxNum }}
            </div>
            <div class="middle">手机号： {{ item.cusMobile }}</div>
            <div class="footer">
              <div class="left">
                <div class="cus">卡号： {{ item.extend1 || '' }}</div>
              </div>

              <div class="right">
                <div class="btn" v-if="!item.extend1" @click="toRelectedCard(item)">绑卡</div>
                <div
                  class="status"
                  :style="{ color: item.processColor }"
                  @click.stop="changeMarker(item)"
                >
                  {{ item.processName }}
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div v-if="cusResList.length === 0" class="no-text">暂无数据</div>
    </van-pull-refresh>
  </div>
  <van-overlay :show="showRelectedCardBox" @click="showRelectedCardBox = false">
    <div class="CardBox">
      <div class="content" @click.stop>
        <div class="title">
          <div class="line"></div>
          <div>绑定IC卡</div>
          <div class="line"></div>
        </div>
        <div class="cusName">
          {{ selectedDataName }}
        </div>
        <div class="getCardBox">
          <div class="number">请放置IC卡后点击绑卡按钮识别</div>
          <div class="btn" @click="toGetCard">绑卡</div>
        </div>
        <div class="cardInput">
          卡号：
          <div class="inputBox">{{ makeSureCardNumber }}</div>
        </div>
        <div class="footer">
          <div class="cancel" @click="showRelectedCardBox = false">取消</div>
          <div class="ok" @click="submitCard">确认</div>
        </div>
      </div>
    </div>
  </van-overlay>
</template>

<script setup>
import api from '@/api/index'
import { getCodeValue } from '@/libs/code'
import { ref, onMounted } from 'vue'
import { showNotify } from 'vant'
import { useRouter } from 'vue-router'
const showRelectedCardBox = ref(false)
const searchForm = {
  status: '',
  cusType: '2',
  pageInfo: {
    pageSize: 100,
    pageNum: 1
  }
}
const selectedDataName = ref('')
const selectedDataId = ref('')
// const getIdentifCardNumber = ref('请放置IC卡后点击绑卡按钮识别')
const makeSureCardNumber = ref('')
const loading = ref(false)
const cusResList = ref([])
const selected = ref('2')
const getList = () => {
  cusResList.value = []
  cusResList.value = []
  let tempArr = []
  api
    .cusList(searchForm)
    .then(async (res) => {
      for (const item of res.data) {
        if (item.extend1 === '' || item.extend1 === null) {
          item.processName = '未绑定'
          item.processColor = ' #f59a23'
        } else {
          item.processName = '已绑定'
          item.processColor = ' #03A113'
        }
        item.cusClassifyName = await getCodeValue('CUS_CLASSIFY', item.cusClassify)
        tempArr.push(item)
      }
      console.log(tempArr)
      if (tempArr.length > 0) {
        cusResList.value.push(tempArr)
        tempArr = []
      }
    })
    .finally(() => {
      loading.value = false
    })
}
const switchCus = (val) => {
  if (val == '1') {
    selected.value = '1'
    searchForm.cusType = '1'
  } else {
    selected.value = '2'
    searchForm.cusType = '2'
  }
  getList()
}
onMounted(() => {
  getList()
})

const toRelectedCard = (item) => {
  showRelectedCardBox.value = true
  selectedDataName.value = item.cusName
  selectedDataId.value = item.id
  makeSureCardNumber.value = ''
}
const toGetCard = () => {
  // window.icCardLib.readCardId();
  // showRelectedCardBox下面的代码要上一体机才可测试
  // if('返回的卡号就展示不然给他报个错'){
  //   getIdentifCardNumber.value='返回的值';
  //   makeSureCardNumber.value='返回的值';
  // }else{
  //   showNotify({ type: 'warning', message: '查询不到卡号！' });
  // }
  const number = window.icCardLib.readCardId()
  if (!number) {
    showNotify({
      message: '卡号读取失败',
      type: 'error'
    })
  } else {
    makeSureCardNumber.value = number
  }
  // showNotify({ type: 'warning', message: '查询不到卡号！' })
}
const submitCard = () => {
  // 传客商id和绑定卡号给后台
  if (!makeSureCardNumber.value) {
    showNotify({ type: 'warning', message: '请先识别卡号！' })
  } else {
    api
      .cusRealeatedCard({ id: selectedDataId.value, icCardNo: makeSureCardNumber.value })
      .then(() => {
        showNotify({ type: 'success', message: '操作成功' });
        showRelectedCardBox.value=false;
        getList()
      })
  }
}
const router = useRouter()
function toDetail(item) {
  router.push({
    path: '/CusDetail',
    query: {
      id: item.id
    }
  })
}
function toAddCus() {
  router.push({
    path: '/CusDetail',
    query: {
      // selected 类别 值2是供应商，1是客户，跳转新增，供应商默认性质cusClassify为个人，客户默认为企业
      cusType: selected.value
    }
  })
}
function cusCancel() {
  window.android.cusCancel()
}
</script>

<style lang="scss" scope>
@import url('@/assets/style/mixins.scss');
@media screen and (max-width: 1080px) {
  .order {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    background-color: #ffffff;
    .cusBoxHeader {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      padding: 0.1rem 0.16rem;
      font-size: 0.3rem;
      padding: 0.3rem 0.2rem 0.2rem 0.2rem;
      align-items: center;
      .cusSwitchBox {
        display: flex;
        flex-direction: row;
        width: 3rem;
        .labelName {
          flex: 1;
          display: flex;
          justify-content: center;
          color: white;
          background: #169bd5;
          padding: 5px 0;
        }
      }
      .btn {
        background: #169bd5;
        color: white;
        border-radius: 5px;
        padding: 3px 5px;
      }
    }
    .list {
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: stretch;
      overflow-y: auto;
      .no-text {
        font-size: 40px;
        color: #999;
        text-align: center;
        margin-top: 200px;
      }
      .row {
        display: flex;
        flex-direction: row;
        padding: 0.25rem 0.2rem 0.25rem 0.2rem;
        border-bottom: 1px solid #eeeeee;
        .img {
          width: 70px;
          height: 102px;
          margin-right: 0.2rem;
          border-radius: 2px;
        }
        .msg {
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          flex: 1;
          width: 0;
          .title {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            font-size: 12px;
            align-items: center;
            margin-right: 1rem;
            .no {
              display: flex;
              flex-direction: row;
              align-items: center;
              justify-content: flex-start;
            }
            .blue {
              color: #02a7f0;
            }
            .maxWidth {
              max-width: 5rem;
              white-space: nowrap; /* 禁止文本换行 */
              overflow: hidden; /* 隐藏溢出内容 */
              text-overflow: ellipsis; /* 溢出部分显示省略号 */
              display: inline-block; /* 确保是块级元素 */
              //overflow: auto; 省略号变成横向拖动就换这个代码
            }
            .status {
              font-weight: bold;
            }
          }
          .middle {
            display: flex;
            flex-direction: column;
            flex: 1;
            font-size: 12px;
          }
          .footer {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            align-items: center;
            font-size: 12px;
            height: 20px;
            .left {
              display: flex;
              flex-direction: row;
              height: 100%;
              .number {
                width: 70px;
                background-color: #44ed5f;
                display: flex;
                flex-direction: row;
                align-items: center;
                justify-content: center;
                border-radius: 2px;
                height: 100%;
              }
              .cus {
                display: flex;
                flex-direction: row;
                align-items: center;
                // justify-content: center;
                height: 100%;
                text-align: center;
              }
            }
            .right {
              display: flex;
              flex-direction: row;
              align-items: center;
              font-size: 12px;
              justify-content: space-between;
              .btn {
                border-radius: 4px;
                color: #1e98d7;
                border: 1px solid #1e98d7;
                height: 0.4rem;
                font-size: 11px;
                padding: 0 0.2rem;
              }
              .status {
                margin-left: 8px;
              }
            }
          }
        }
      }
    }
  }
  .CardBox {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    .content {
      width: 5rem;
      height: 3.5rem;
      background: #ffffff;
      border-radius: 15px;
      display: flex;
      flex-direction: column;
      align-items: center;
      font-size: 0.3rem;
      padding: 0.2rem;
      .title {
        color: #1e98d7;
        margin: 0.1rem 0;
        display: flex;
        flex-direction: row;
        .line {
          border-top: 1.5px solid #1e98d7;
          height: 0.1rem;
          width: 0.8rem;
          margin: 0.1rem;
        }
      }
      .getCardBox {
        display: flex;
        flex-direction: row;
        font-size: 0.25rem;
        justify-content: space-between;
        align-items: center;
        margin: 0.3rem 0;
        .number {
          color: #666666;
        }
        .btn {
          background: white;
          color: #169bd5;
          border-radius: 5px;
          padding: 0.04rem 0.2rem;
          margin-left: 0.2rem;
          border: 1px solid #169bd5;
          font-size: 0.2rem;
        }
      }
      .cardInput {
        display: flex;
        flex-direction: row;
        height: 1rem;
        .inputBox {
          border: 1px solid #666666;
          height: 100%;
          width: 3rem;
          border-radius: 2px;
        }
      }
      .footer {
        display: flex;
        flex-direction: row;
        justify-content: space-evenly;
        margin-top: 0.5rem;
        font-size: 0.2rem;
        .cancel {
          color: #169bd5;
          border: 1px solid #169bd5;
          width: 1rem;
          display: flex;
          flex-direction: row;
          justify-content: center;
          align-items: center;
          border-radius: 5px;
          height: 0.5rem;
        }
        .ok {
          background: #1e98d7;
          color: white;
          border-radius: 5px;
          width: 1rem;
          display: flex;
          flex-direction: row;
          justify-content: center;
          align-items: center;
          /* flex: 1; */
          height: 0.5rem;
        }
      }
    }
  }
}
@media screen and (min-width: 1920px) {
  .order {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    background-color: #ffffff;
    .cusBoxHeader {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      font-size: 0.15rem;
      padding: 0.1rem;
      align-items: center;
      .cusSwitchBox {
        display: flex;
        flex-direction: row;
        width: 1.5rem;
        .labelName {
          flex: 1;
          display: flex;
          justify-content: center;
          color: white;
          background: #169bd5;
          padding: 3px 0;
        }
      }
      .btn {
        background: #169bd5;
        color: white;
        border-radius: 2.5px;
        padding: 1.5px 2.5px;
      }
    }
    .list {
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: stretch;
      overflow-y: auto;
      .no-text {
        font-size: 40px;
        color: #999;
        text-align: center;
        margin-top: 200px;
      }
      .rows{
        display: flex;
        flex-wrap: wrap; /* 关键：允许换行 */
      }
      .row {
        display: flex;
        flex-direction: row;
        padding: 0.1rem 0.2rem 0.1rem 0.2rem;
        border-bottom: 1px solid #eeeeee;
        width: 4.6rem;
        .img {
          width: 70px;
          height: 102px;
          margin-right: 0.2rem;
          border-radius: 2px;
        }
        .msg {
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          flex: 1;
          //width: 0;
          .title {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            font-size: 6px;
            align-items: center;
            margin-right: 0.5rem;
            .no {
              display: flex;
              flex-direction: row;
              align-items: center;
              justify-content: flex-start;
            }
            .blue {
              color: #02a7f0;
            }
            .maxWidth {
              max-width: 2.5rem;
              white-space: nowrap; /* 禁止文本换行 */
              overflow: hidden; /* 隐藏溢出内容 */
              text-overflow: ellipsis; /* 溢出部分显示省略号 */
              display: inline-block; /* 确保是块级元素 */
              //overflow: auto; 省略号变成横向拖动就换这个代码
            }
            .status {
              font-weight: bold;
            }
          }
          .middle {
            display: flex;
            flex-direction: column;
            flex: 1;
            font-size: 6px;
          }
          .footer {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            align-items: center;
            font-size: 6px;
            height: 10px;
            margin: 0;
            .left {
              display: flex;
              flex-direction: row;
              height: 100%;
              .number {
                width: 35px;
                background-color: #44ed5f;
                display: flex;
                flex-direction: row;
                align-items: center;
                justify-content: center;
                border-radius: 2px;
                height: 100%;
              }
              .cus {
                display: flex;
                flex-direction: row;
                align-items: center;
                // justify-content: center;
                height: 100%;
                text-align: center;
              }
            }
            .right {
              display: flex;
              flex-direction: row;
              align-items: center;
              font-size: 6px;
              justify-content: space-between;
              .btn {
                border-radius: 4px;
                color: #1e98d7;
                border: 0.5px solid #1e98d7;
                height: 0.3rem;
                font-size: 5.5px;
                padding: 0 0.1rem;
              }
              .status {
                margin-left: 4px;
              }
            }
          }
        }
      }
    }
  }
  .CardBox {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    .content {
      width: 5rem;
      height: 3.5rem;
      background: #ffffff;
      border-radius: 15px;
      display: flex;
      flex-direction: column;
      align-items: center;
      font-size: 0.3rem;
      padding: 0.2rem;
      .title {
        color: #1e98d7;
        margin: 0.1rem 0;
        display: flex;
        flex-direction: row;
        .line {
          border-top: 1.5px solid #1e98d7;
          height: 0.1rem;
          width: 0.8rem;
          margin: 0.1rem;
        }
      }
      .getCardBox {
        display: flex;
        flex-direction: row;
        font-size: 0.25rem;
        justify-content: space-between;
        align-items: center;
        margin: 0.3rem 0;
        .number {
          color: #666666;
        }
        .btn {
          background: white;
          color: #169bd5;
          border-radius: 5px;
          padding: 0.04rem 0.2rem;
          margin-left: 0.2rem;
          border: 1px solid #169bd5;
          font-size: 0.2rem;
        }
      }
      .cardInput {
        display: flex;
        flex-direction: row;
        height: 1rem;
        .inputBox {
          border: 1px solid #666666;
          height: 100%;
          width: 3rem;
          border-radius: 2px;
        }
      }
      .footer {
        display: flex;
        flex-direction: row;
        justify-content: space-evenly;
        margin-top: 0.5rem;
        font-size: 0.2rem;
        .cancel {
          color: #169bd5;
          border: 1px solid #169bd5;
          width: 1rem;
          display: flex;
          flex-direction: row;
          justify-content: center;
          align-items: center;
          border-radius: 5px;
          height: 0.5rem;
        }
        .ok {
          background: #1e98d7;
          color: white;
          border-radius: 5px;
          width: 1rem;
          display: flex;
          flex-direction: row;
          justify-content: center;
          align-items: center;
          /* flex: 1; */
          height: 0.5rem;
        }
      }
    }
  }
}
</style>
